package com.ssyt.hiring.talent.service;

import com.ssyt.hiring.common.Constants;
import com.ssyt.hiring.common.util.DBUtil;
import com.ssyt.hiring.company.entity.CompanyEntity;
import com.ssyt.hiring.talent.entity.TalentEntity;

public class TalentService {

	/**
	 * 检查邮箱是否存在
	 * 
	 * @param accountType
	 * @param email
	 * @return
	 */
	public boolean isExisEmail(String accountType, String email) {
		boolean result = false;
		if (Constants.HIRING_TALENT.equals(accountType)) {
			String sql = "SELECT * FROM talent WHERE email=?";
			TalentEntity talent = DBUtil.getUniqueResult(TalentEntity.class,
					sql, email);
			if (talent != null) {
				result = true;
			}
		} else if (Constants.HIRING_COMPANY.equals(accountType)) {
			String sql = "SELECT * FROM company WHERE email=?";
			CompanyEntity company = DBUtil.getUniqueResult(CompanyEntity.class,
					sql, email);
			if (company != null) {
				result = true;
			}
		} else {
			throw new RuntimeException("不存在用户类型：" + accountType);
		}
		return result;
	}

	/**
	 * 更新邮件过期时间和密钥
	 * 
	 * @param email
	 * @param accountType
	 * @param millis
	 * @param securityKey
	 */
	public void updateSecurityKeyAndEmailLinkOutDate(String email,
			String accountType, long millis, String securityKey) {

		if (Constants.HIRING_TALENT.equals(accountType)) {

			String sql = "UPDATE talent SET email_link_out_date=?,security_key=? WHERE email=?";
			DBUtil.executeUpdate(sql, millis, securityKey, email);

		} else if (Constants.HIRING_COMPANY.equals(accountType)) {

			String sql = "UPDATE company SET email_link_out_date=?,security_key=? WHERE email=?";
			DBUtil.executeUpdate(sql, millis, securityKey, email);

		} else {
			throw new RuntimeException("不存在用户类型：" + accountType);
		}
	}

	/**
	 * 判断用户邮箱链接是否过期
	 * 
	 * @param email
	 * @param accountType
	 * @return false 没过期，true 过期
	 */
	public boolean isEmailLinkExpired(String email, String accountType) {
		boolean result = false;
		if (Constants.HIRING_TALENT.equals(accountType)) {

			String sql = "SELECT email_link_out_date FROM talent WHERE email=?";
			TalentEntity talent = DBUtil.getUniqueResult(TalentEntity.class,
					sql, email);
			if (talent == null
					|| talent.getEmailLinkOutDate() < System
							.currentTimeMillis()) {
				result = true;
			}

		} else if (Constants.HIRING_COMPANY.equals(accountType)) {
			String sql = "SELECT email_link_out_date FROM company WHERE email=?";
			CompanyEntity company = DBUtil.getUniqueResult(CompanyEntity.class,
					sql, email);
			if (company == null
					|| company.getEmailLinkOutDate() < System
							.currentTimeMillis()) {
				result = true;
			}

		} else {
			throw new RuntimeException("不存在用户类型：" + accountType);
		}
		return result;
	}

	/**
	 * 判断邮箱和密钥是否匹配
	 * 
	 * @param email
	 * @param accountType
	 * @param securityKey
	 * @return
	 */
	public boolean isMatched(String email, String accountType,
			String securityKey) {
		boolean result = false;
		if (Constants.HIRING_TALENT.equals(accountType)) {

			String sql = "SELECT * FROM talent WHERE email=? and security_key=?";
			TalentEntity talent = DBUtil.getUniqueResult(TalentEntity.class,
					sql, email, securityKey);
			if (talent != null) {
				result = true;
			}

		} else if (Constants.HIRING_COMPANY.equals(accountType)) {
			String sql = "SELECT * FROM company WHERE email=? and security_key=?";
			CompanyEntity company = DBUtil.getUniqueResult(CompanyEntity.class,
					sql, email, securityKey);
			if (company != null) {
				result = true;
			}

		} else {
			throw new RuntimeException("不存在用户类型：" + accountType);
		}
		return result;
	}

	/**
	 * 找回密码-重置密码
	 * 
	 * @param accountType
	 * @param email
	 * @param newPassword
	 */
	public void updatePassword(String accountType, String email,
			String newPassword) {
		if (Constants.HIRING_TALENT.equals(accountType)) {

			String sql = "UPDATE talent SET password=? WHERE email=?";
			DBUtil.executeUpdate(sql, newPassword, email);

		} else if (Constants.HIRING_COMPANY.equals(accountType)) {

			String sql = "UPDATE company SET password=? WHERE email=?";
			DBUtil.executeUpdate(sql, newPassword, email);

		} else {
			throw new RuntimeException("不存在用户类型：" + accountType);
		}

	}

}
